/*=========================================================================

Program:   Virtual Surgery
Language:  C++
Date:      2009-9-1
Version:   0.1
Mail:		   nelfylee@gmail.com

Copyright (c) 2009 Nelfy Lee. All rights reserved.

=========================================================================*/
#ifndef _VOLUME_SOURCE_H_
#define _VOLUME_SOURCE_H_

#include "Common.h"
#include "Source.h"

#include <QDebug>
#include <QFileDialog>

#include "itkImageRegion.h"
#include "itkImageDuplicator.h"
#include "itkImageFileWriter.h"

#include "vtkVolumeRayCastMapper.h"
#include "vtkUnstructuredGrid.h"
#include "vtkFixedPointVolumeRayCastMapper.h"




class VolumeSourceBase : public Source
{
public:
	VolumeSourceBase(QString text);
	~VolumeSourceBase(){}

	virtual void setUpperThreshold(double th);
	virtual void setLowerThreshold(double th);
	virtual void setThresholdBetween(double lo, double up);
	virtual double getUpperThreshold();
	virtual double getLowerThreshold();

	virtual void setVolumeOpacity(QPolygonF points);
	virtual vtkProp* sourceModel(bool refresh = false);

protected:
	double m_upperThreshold;
	double m_lowerThreshold;

	double m_sourceWindow;			// window/level 
	double m_sourceLevel;			// window/level

	// volume model
	vtkImageCast *m_vmImageCast;
	vtkPiecewiseFunction *m_vmOpacityTransferFunction;
	vtkColorTransferFunction *m_vmColorTransferFunction;
	vtkVolumeProperty *m_vmVolumeProperty;
	vtkVolumeRayCastCompositeFunction *m_vmVolumeRayCastCompositeFunction;
	vtkVolumeRayCastMapper *m_vmVolumeRayCastMapper;
	vtkVolume *m_vmVolume;
};



template<class TImageType>
class VolumeSource : public VolumeSourceBase
{
protected:
    
	typedef itk::ImageRegionIterator<TImageType> Iterator3D;
	typedef itk::ImageToVTKImageFilter<TImageType> Connector;

public:
	VolumeSource(QString name);

	~VolumeSource();

	void setVolume(typename TImageType::Pointer iImage);

	void setData(vtkImageData*);

	typename TImageType::Pointer getVolume()
	{
		return m_itkImage;
	}

private:
	typename TImageType::Pointer m_itkImage;
};



//////////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////////


template<class TImageType> 
VolumeSource<TImageType>::VolumeSource(QString text) : VolumeSourceBase(text)
{
	m_type = stVolume;
	m_itkImage = NULL;
}


template<class TImageType> 
VolumeSource<TImageType>::~VolumeSource()
{ 
}


template<class TImageType> void
VolumeSource<TImageType>::setVolume(typename TImageType::Pointer iImage)
{
	typedef itk::ImageDuplicator<TImageType> ImageDuplicatorFilter;
	ImageDuplicatorFilter::Pointer dup = ImageDuplicatorFilter::New();
	dup->SetInputImage(iImage);
	dup->Update();

	m_itkImage = dup->GetOutput();

	typename Connector::Pointer connector = Connector::New();
	connector->SetInput(m_itkImage);
	connector->Update();

	if ( m_vtkImage)
		m_vtkImage->Delete();
	
	m_vtkImage = vtkImageData::New();

	m_vtkImage->DeepCopy(connector->GetOutput());
	m_vtkImage->SetOrigin(0,0,0);

	m_dataType = m_vtkImage->GetScalarType();
	//qDebug("datatype:%d", m_dataType);

	double range[2];

	m_vtkImage->GetScalarRange(range);

	m_sourceWindow = (range[1] - range[0]);
	m_sourceLevel = (range[1] + range[0])/2.0;

	m_upperThreshold = range[1];
	m_lowerThreshold = range[0];
}


#endif


